package com.youtui.common.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
/** jedis������� */
@SuppressWarnings("deprecation")
public class JedisUtil {

    private Logger logger = LoggerFactory.getLogger(JedisUtil.class);

    public static int seconds_m = 60;
    public static int seconds_h = 60 * 60;
    public static int seconds_d = 24 * 60 * 60;

    @Autowired
    private JedisPool jedisPool;
    
    @Value("${redis_index}")
    private int redis_index;

    /**
 
     */
    public String getStr(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool();
            jedis.select(2);
            value = jedis.get(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return value;
    }

    /**  
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.set(key, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return "0";
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    public boolean setStr(String key, String value, int seconds, boolean override) {

        Jedis jedis = null;
        boolean ok = false;

        try {
            jedis = jedisPool();

            if (override == false && jedis.exists(key)) {
                ok = false;
            } else {
                if (seconds < 1) {
                    jedis.set(key, value);
                } else {
                    jedis.setex(key, seconds, value);
                }
                ok = true;
            }
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            ok = false;
        } finally {
            returnResource(jedisPool, jedis);
        }
        return ok;
    }

    public boolean setObj(String key, Object value, int seconds, boolean override) {
        Jedis jedis = null;
        boolean ok = false;
        byte[] data = SerializeUtil.serialize(value);
        if (data == null) {
            if (logger.isInfoEnabled()) {
                logger.info("setObj SerialUtil.encode error");
            }
            return false;
        }

        try {
            jedis = jedisPool();
            if (override == false && jedis.exists(key)) {
                ok = false;
            } else {
                if (seconds < 1) {
                    jedis.set(key.getBytes(), data);
                } else {
                    jedis.setex(key.getBytes(), seconds, data);
                }
                ok = true;
            }
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            ok = false;
        } finally {
            returnResource(jedisPool, jedis);
        }
        return ok;
    }

    /*
     * (�� Javadoc) <p>Title: getObj</p> <p>Description: </p>
     * 
     * @param key
     * 
     * @return
     * 
     * @see com.shangying.service.business.test#getObj(java.lang.String)
     */
    public Object getObj(String key) {
        byte[] data = null;
        Jedis jedis = null;

        try {
            jedis = jedisPool();
            data = jedis.get(key.getBytes());
            if (data == null || data.length == 0) {
                if (logger.isDebugEnabled()) {
                    logger.debug("redis data is empty " + key);
                }
            }
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        Object obj = null;
        if (data != null) {
            obj = SerializeUtil.unserialize(data);
        }
        return obj;
    }

    /**
     * ɾ��ָ����key,Ҳ���Դ���һ����key������
     * 
     * @param keys
     *            һ��key Ҳ����ʹ string ����
     * @return ����ɾ��ɹ��ĸ���
     */
    public Long del(String... keys) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.del(keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * ɾ��ָ����key,Ҳ���Դ���һ����key������
     * 
     * @param keys
     *            һ��key Ҳ����ʹ string ����
     * @return ����ɾ��ɹ��ĸ���
     */
    public Long del(byte[] key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.del(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * ͨ��key��ָ����valueֵ׷��ֵ
     * </p>
     * 
     * @param key
     * @param str
     * @return �ɹ����� ��Ӻ�value�ĳ��� ʧ�� ���� ��ӵ� value �ĳ��� �쳣����0L
     */
    public Long append(String key, String str) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.append(key, str);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * �ж�key�Ƿ����
     * </p>
     * 
     * @param key
     * @return true OR false
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.exists(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return false;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * �ж�key�Ƿ����
     * </p>
     * 
     * @param key
     * @return true OR false
     */
    public Boolean exists(byte[] key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.exists(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return false;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * ����key value,���key�Ѿ������򷵻�0,nx==> not exist
     * </p>
     * 
     * @param key
     * @param value
     * @return �ɹ�����1 ������ �� �����쳣 ���� 0
     */
    public Long setnx(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.setnx(key, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * <p>
     * ����key value���ƶ������ֵ����Ч��
     * </p>
     * 
     * @param key
     * @param value
     * @param seconds
     *            ��λ:��
     * @return �ɹ�����OK ʧ�ܺ��쳣����null
     */
    public String setex(String key, String value, int seconds) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.setex(key, seconds, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key ��offset ��ָ����λ�ÿ�ʼ��ԭ��value�滻 �±��0��ʼ,offset��ʾ��offset�±꿪ʼ�滻
     * ����滻���ַ��ȹ�С������� example: value : bigsea@zto.cn str : abc ���±�7��ʼ�滻 ����Ϊ
     * RES : bigsea.abc.cn
     * 
     * @param key
     * @param str
     * @param offset
     *            �±�λ��
     * @return �����滻�� value �ĳ���
     */
    public Long setrange(String key, String str, int offset) {
        Jedis jedis = null;
        try {
            jedis = jedisPool();
            return jedis.setrange(key, offset, str);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }

    /**
     * ͨ��������key��ȡ������value
     * 
     * @param keys
     *            string���� Ҳ������һ��key
     * @return �ɹ�����value�ļ���, ʧ�ܷ���null�ļ��� ,�쳣���ؿ�
     */
    public List<String> mget(String... keys) {
        Jedis jedis = null;
        List<String> values = null;
        try {
            jedis = jedisPool();
            values = jedis.mget(keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return values;
    }

    /**
     * ����������key:value,����һ�� example: obj.mset(new
     * String[]{"key2","value1","key2","value2"})
     * 
     * @param keysvalues
     * @return �ɹ�����OK ʧ�� �쳣 ���� null
     * 
     */
    public String mset(String... keysvalues) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.mset(keysvalues);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ����������key:value,����һ��,���key�Ѿ��������ʧ��,������ع�
     * </p>
     * <p>
     * example:
     * </p>
     * <p>
     * obj.msetnx(new String[]{"key2","value1","key2","value2"})
     * </p>
     * 
     * @param keysvalues
     * @return �ɹ�����1 ʧ�ܷ���0
     */
    public Long msetnx(String... keysvalues) {
        Jedis jedis = null;
        Long res = 0L;
        try {
            jedis = jedisPool();
            res = jedis.msetnx(keysvalues);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ����key��ֵ,������һ����ֵ
     * </p>
     * 
     * @param key
     * @param value
     * @return ��ֵ ���key������ �򷵻�null
     */
    public String getset(String key, String value) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.getSet(key, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ���±� ��key ��ȡָ���±�λ�õ� value
     * </p>
     * 
     * @param key
     * @param startOffset
     *            ��ʼλ�� ��0 ��ʼ �����ʾ���ұ߿�ʼ��ȡ
     * @param endOffset
     * @return ���û�з���null
     */
    public String getrange(String key, int startOffset, int endOffset) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.getrange(key, startOffset, endOffset);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key ��value���м�ֵ+1����,��value����int����ʱ�᷵�ش���,��key����������valueΪ1
     * </p>
     * 
     * @param key
     * @return ��ֵ��Ľ��
     */
    public Long incr(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.incr(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��ָ����value��ֵ,���key������,������valueΪ��ֵ
     * </p>
     * 
     * @param key
     * @param integer
     * @return
     */
    public Long incrBy(String key, Long integer) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.incrBy(key, integer);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ��key��ֵ����������,���key������,������keyΪ-1
     * </p>
     * 
     * @param key
     * @return
     */
    public Long decr(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.decr(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ��ȥָ����ֵ
     * </p>
     * 
     * @param key
     * @param integer
     * @return
     */
    public Long decrBy(String key, Long integer) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.decrBy(key, integer);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��ȡvalueֵ�ĳ���
     * </p>
     * 
     * @param key
     * @return ʧ�ܷ���null
     */
    public Long serlen(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.strlen(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��field����ָ����ֵ,���key������,���ȴ���
     * </p>
     * 
     * @param key
     * @param field
     *            �ֶ�
     * @param value
     * @return �����ڷ���0 �쳣����null
     */
    public Long hset(String key, String field, String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.hset(key, field, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��field����ָ����ֵ,���key���������ȴ���,���field�Ѿ�����,����0
     * </p>
     * 
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hsetnx(String key, String field, String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.hsetnx(key, field, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��keyͬʱ���� hash�Ķ��field
     * </p>
     * 
     * @param key
     * @param hash
     * @return ����OK �쳣����null
     */
    public String hmset(String key, Map<String, String> hash) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.hmset(key, hash);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key �� field ��ȡָ���� value
     * </p>
     * 
     * @param key
     * @param field
     * @return û�з���null
     */
    public String hget(String key, String field) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.hget(key, field);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key �� fields ��ȡָ����value ���û�ж�Ӧ��value�򷵻�null
     * </p>
     * 
     * @param key
     * @param fields
     *            ����ʹ һ��String Ҳ������ String����
     * @return
     */
    public List<String> hmget(String key, String... fields) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.hmget(key, fields);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��ָ����field��value���ϸ��ֵ
     * </p>
     * 
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hincrby(String key, String field, Long value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.hincrBy(key, field, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��field�ж��Ƿ���ָ����value����
     * </p>
     * 
     * @param key
     * @param field
     * @return
     */
    public Boolean hexists(String key, String field) {
        Jedis jedis = null;
        Boolean res = false;
        try {
            jedis = jedisPool();
            res = jedis.hexists(key, field);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key����field������
     * </p>
     * 
     * @param key
     * @return
     */
    public Long hlen(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.hlen(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;

    }

    /**
     * <p>
     * ͨ��key ɾ��ָ���� field
     * </p>
     * 
     * @param key
     * @param fields
     *            ������ һ�� field Ҳ������ һ������
     * @return
     */
    public Long hdel(String key, String... fields) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.hdel(key, fields);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key�������е�field
     * </p>
     * 
     * @param key
     * @return
     */
    public Set<String> hkeys(String key) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.hkeys(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key�������к�key�йص�value
     * 
     * @param key
     * @return
     */
    public List<String> hvals(String key) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.hvals(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡ���е�field��value
     * 
     * @param key
     * @return
     */
    public Map<String, String> hgetall(String key) {
        Jedis jedis = null;
        Map<String, String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.hgetAll(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��listͷ������ַ�
     * </p>
     * 
     * @param key
     * @param strs
     *            ����ʹһ��string Ҳ����ʹstring����
     * @return ����list��value����
     */
    public Long lpush(String key, String... strs) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.lpush(key, strs);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��listβ������ַ�
     * </p>
     * 
     * @param key
     * @param strs
     *            ����ʹһ��string Ҳ����ʹstring����
     * @return ����list��value����
     */
    public Long rpush(String key, String... strs) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.rpush(key, strs);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    // /**
    // * <p>ͨ��key��listָ����λ��֮ǰ����֮�� ����ַ�Ԫ��</p>
    // * @param key
    // * @param where LIST_POSITIONö������
    // * @param pivot list�����value
    // * @param value ��ӵ�value
    // * @return
    // */
    // public Long linsert(String key, LIST_POSITION where, String pivot, String
    // value) {
    // Jedis jedis = null;
    // Long res = null;
    // try {
    // jedis = jedisPool();
    // res = jedis.linsert(key, where, pivot, value);
    // } catch (Exception e) {
    // jedisPool.returnBrokenResource(jedis);
    // e.printStackTrace();
    // } finally {
    // returnResource(jedisPool, jedis);
    // }
    // return res;
    // }

    /**
     * ͨ��key����listָ���±�λ�õ�value ����±곬��list����value�ĸ����򱨴�
     * 
     * @param key
     * @param index
     *            ��0��ʼ
     * @param value
     * @return �ɹ�����OK
     */
    public String lset(String key, Long index, String value) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.lset(key, index, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key�Ӷ�Ӧ��list��ɾ��ָ����count�� �� value��ͬ��Ԫ��
     * 
     * @param key
     * @param count
     *            ��countΪ0ʱɾ��ȫ��
     * @param value
     * @return ���ر�ɾ��ĸ���
     */
    public Long lrem(String key, long count, String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.lrem(key, count, value);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����list�д�strat�±꿪ʼ��end�±�����valueֵ
     * 
     * @param key
     * @param start
     * @param end
     * @return �ɹ�����OK
     */
    public String ltrim(String key, long start, long end) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.ltrim(key, start, end);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��list��ͷ��ɾ��һ��value,�����ظ�value
     * 
     * @param key
     * @return
     */
    public String lpop(String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.lpop(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��listβ��ɾ��һ��value,�����ظ�Ԫ��
     * 
     * @param key
     * @return
     */
    public String rpop(String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.rpop(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��һ��list��β��ɾ��һ��value����ӵ���һ��list��ͷ��,�����ظ�value ����һ��listΪ�ջ��߲������򷵻�null
     * 
     * @param srckey
     * @param dstkey
     * @return
     */
    public String rpoplpush(String srckey, String dstkey) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.rpoplpush(srckey, dstkey);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡlist��ָ���±�λ�õ�value
     * 
     * @param key
     * @param index
     * @return ���û�з���null
     */
    public String lindex(String key, long index) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.lindex(key, index);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����list�ĳ���
     * 
     * @param key
     * @return
     */
    public Long llen(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.llen(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡlistָ���±�λ�õ�value ���start Ϊ 0 end Ϊ -1 �򷵻�ȫ����list�е�value
     * 
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<String> lrange(String key, long start, long end) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.lrange(key, start, end);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ָ����set�����value
     * 
     * @param key
     * @param members
     *            ������һ��String Ҳ������һ��String����
     * @return ��ӳɹ��ĸ���
     */
    public Long sadd(String key, String... members) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.sadd(key, members);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��keyɾ��set�ж�Ӧ��valueֵ
     * 
     * @param key
     * @param members
     *            ������һ��String Ҳ������һ��String����
     * @return ɾ��ĸ���
     */
    public Long srem(String key, String... members) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.srem(key, members);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key���ɾ��һ��set�е�value�����ظ�ֵ
     * </p>
     * 
     * @param key
     * @return
     */
    public String spop(String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.spop(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * <p>
     * ͨ��key��ȡset�еĲ
     * </p>
     * <p>
     * �Ե�һ��setΪ��׼
     * </p>
     * 
     * @param keys
     *            ����ʹһ��string �򷵻�set�����е�value Ҳ������string����
     * @return
     */
    public Set<String> sdiff(String... keys) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.sdiff(keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡset�еĲ�����뵽��һ��key�� �Ե�һ��setΪ��׼
     * 
     * @param dstkey
     *            ������key
     * @param keys
     *            ����ʹһ��string �򷵻�set�����е�value Ҳ������string����
     * @return
     */
    public Long sdiffstore(String dstkey, String... keys) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.sdiffstore(dstkey, keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡָ��set�еĽ���
     * 
     * @param keys
     *            ����ʹһ��string Ҳ������һ��string����
     * @return
     */
    public Set<String> sinter(String... keys) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.sinter(keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡָ��set�еĽ��� �����������µ�set��
     * 
     * @param dstkey
     * @param keys
     *            ����ʹһ��string Ҳ������һ��string����
     * @return
     */
    public Long sinterstore(String dstkey, String... keys) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.sinterstore(dstkey, keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��������set�Ĳ���
     * 
     * @param keys
     *            ����ʹһ��string Ҳ������һ��string����
     * @return
     */
    public Set<String> sunion(String... keys) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.sunion(keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��������set�Ĳ���,�����뵽�µ�set��
     * 
     * @param dstkey
     * @param keys
     *            ����ʹһ��string Ҳ������һ��string����
     * @return
     */
    public Long sunionstore(String dstkey, String... keys) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.sunionstore(dstkey, keys);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��set�е�value�Ƴ���ӵ��ڶ���set��
     * 
     * @param srckey
     *            ��Ҫ�Ƴ��
     * @param dstkey
     *            ��ӵ�
     * @param member
     *            set�е�value
     * @return
     */
    public Long smove(String srckey, String dstkey, String member) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.smove(srckey, dstkey, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡset��value�ĸ���
     * 
     * @param key
     * @return
     */
    public Long scard(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.scard(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key�ж�value�Ƿ���set�е�Ԫ��
     * 
     * @param key
     * @param member
     * @return
     */
    public Boolean sismember(String key, String member) {
        Jedis jedis = null;
        Boolean res = null;
        try {
            jedis = jedisPool();
            res = jedis.sismember(key, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡset������value,��ɾ��Ԫ��
     * 
     * @param key
     * @return
     */
    public String srandmember(String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.srandmember(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡset�����е�value
     * 
     * @param key
     * @return
     */
    public Set<String> smembers(String key) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.smembers(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��zset�����value,score,����score������������� ����value�Ѿ���������score����Ԫ��
     * 
     * @param key
     * @param scoreMembers
     * @return
     */
    public Long zadd(String key, Map<String, Double> scoreMembers) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zadd(key, scoreMembers);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��zset�����value,score,����score������������� ����value�Ѿ���������score����Ԫ��
     * 
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Long zadd(String key, double score, String member) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zadd(key, score, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��keyɾ����zset��ָ����value
     * 
     * @param key
     * @param members
     *            ����ʹһ��string Ҳ������һ��string����
     * @return
     */
    public Long zrem(String key, String... members) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zrem(key, members);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key���Ӹ�zset��value��score��ֵ
     * 
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Double zincrby(String key, double score, String member) {
        Jedis jedis = null;
        Double res = null;
        try {
            jedis = jedisPool();
            res = jedis.zincrby(key, score, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����zset��value������ �±��С��������
     * 
     * @param key
     * @param member
     * @return
     */
    public Long zrank(String key, String member) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zrank(key, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����zset��value������ �±�Ӵ�С����
     * 
     * @param key
     * @param member
     * @return
     */
    public Long zrevrank(String key, String member) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zrevrank(key, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����ȡscore��start��end��zset��value socre�Ӵ�С���� ��startΪ0 endΪ-1ʱ����ȫ��
     * 
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrevrange(String key, long start, long end) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.zrevrange(key, start, end);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����ָ��score��zset�е�value
     * 
     * @param key
     * @param max
     * @param min
     * @return
     */
    public Set<String> zrangebyscore(String key, String max, String min) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.zrevrangeByScore(key, max, min);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����ָ��score��zset�е�value
     * 
     * @param key
     * @param max
     * @param min
     * @return
     */
    public Set<String> zrangeByScore(String key, double max, double min) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.zrevrangeByScore(key, max, min);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ����ָ�������zset��value������
     * 
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Long zcount(String key, String min, String max) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zcount(key, min, max);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key����zset�е�value����
     * 
     * @param key
     * @return
     */
    public Long zcard(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zcard(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key��ȡzset��value��scoreֵ
     * 
     * @param key
     * @param member
     * @return
     */
    public Double zscore(String key, String member) {
        Jedis jedis = null;
        Double res = null;
        try {
            jedis = jedisPool();
            res = jedis.zscore(key, member);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��keyɾ�������ڵ�Ԫ��
     * 
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByRank(String key, long start, long end) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zremrangeByRank(key, start, end);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��keyɾ��ָ��score�ڵ�Ԫ��
     * 
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByScore(String key, double start, double end) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool();
            res = jedis.zremrangeByScore(key, start, end);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ��������pattern���ʽ������key keys(*) �������е�key
     * 
     * @param pattern
     * @return
     */
    public Set<String> keys(String pattern) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool();
            res = jedis.keys(pattern);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ͨ��key�ж�ֵ������
     * 
     * @param key
     * @return
     */
    public String type(String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool();
            res = jedis.type(key);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            e.printStackTrace();
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }

    /**
     * ���������ӳ�
     * 
     * @param pool
     * @param redis
     */
    public static void returnResource(JedisPool pool, Jedis jedis) {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
     
	private Jedis jedisPool(){
    	Jedis jedis = jedisPool.getResource();
    	jedis.select(redis_index);
		return jedis;
    }

}
